package com.chwn.curiousdiary.ui.user.fragment;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;

import com.chwn.curiousdiary.R;
import com.chwn.curiousdiary.adapter.MessageAdapter;
import com.chwn.curiousdiary.bean.MessageBean;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.curiousdiary.ui.user.contract.MessageContract;
import com.chwn.curiousdiary.ui.user.presenter.MessagePresenter;
import com.chwn.curiousdiary.ui.user.activity.MessageDetailActivity;
import com.chwn.harulib.adapter.ViewHolder;
import com.chwn.harulib.adapter.interfaces.OnMultiItemClickListeners;
import com.chwn.harulib.mvp.base.BaseMVPFragment;
import com.chwn.harulib.mvp.rx.HttpResultException;
import com.chwn.harulib.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * Created by Myshikin on 2017/3/24.
 * desc: 消息列表
 */
public class MessageFragment extends BaseMVPFragment<MessagePresenter> implements MessageContract.View,
        XRecyclerView.LoadingListener {

    private XRecyclerView  mMessageRV;
    private MessageAdapter mMessageAdapter;

    /**
     *
     */
    private int     page      = 1;
    private boolean isRefresh = true;

    @Override
    protected int setContentViewId() {
        return R.layout.fragment_only_recyclerview;
    }

    @Override
    protected void initContentView() {
        mMessageRV = (XRecyclerView) mInflateContentView.findViewById(R.id.xrv_message);
        mMessageAdapter = new MessageAdapter(mActivity, null, false);
        mMessageRV.setLoadingMoreEnabled(true);
        mMessageRV.setLoadingListener(this);
        mMessageRV.setLayoutManager(new LinearLayoutManager(mActivity));
        mMessageRV.setAdapter(mMessageAdapter);
        mMessageAdapter.setOnMultiItemClickListener(new OnMultiItemClickListeners<MessageBean>() {
            @Override
            public void onItemClick(ViewHolder viewHolder, MessageBean data, int position, int viewType) {
                if (viewType == Constant.MESSAGE_TYPE) {
                    Intent intent = new Intent(getActivity(), MessageDetailActivity.class);
                    intent.putExtra(Constant.MESSAGE_URL, data.getMessageURL());
                    intent.putExtra(Constant.MESSAGE_ID, data.getMessageId());
                    startActivity(intent);
                }
            }
        });

        initStateViewData();

    }

    private void initStateViewData() {
        setEmptyView(R.drawable.icon_no_message);
        setErrorView(R.drawable.icon_no_message);
        setErrorText("哎呀，快去检查一下网络吧～");
        setEmptyText("您暂时还没有任何消息呢～");
    }

    @Override
    protected void getData() {
        mPresenter.getMessageList(page);
    }

    @Override
    public void onResume() {
        super.onResume();
//        Logger.e("onResume");
        mPresenter.getMessageList(page);
    }

    @Override
    public void onRequestError(Throwable e) {
        super.onRequestError(e);
        if (!isRefresh && e instanceof HttpResultException) {
            mMessageRV.loadMoreComplete();
            mMessageRV.refreshComplete();
            mMessageRV.noMoreLoading();
            page--;
        } else if (e instanceof HttpResultException) {
            mMessageRV.noMoreLoading();
            if (mMessageAdapter.getDatas() == null || mMessageAdapter.getDatas().size() < 1) {
                showEmptyView();
            } else {
                showErrorView();
            }
        } else {
            if (mMessageAdapter.getDatas() == null || mMessageAdapter.getDatas().size() < 1) {
                showErrorView();
            }
        }
    }

    @Override
    public void showMessageList(List<MessageBean> messageList) {
        showContentView();
        if (messageList.size() < Constant.PAGE_COUNT) {
            mMessageRV.noMoreLoading();
        }
        if (isRefresh) {
            mMessageAdapter.setNewData(messageList);
            mMessageRV.refreshComplete();
        } else {
            mMessageAdapter.setLoadMoreData(messageList);
            mMessageRV.loadMoreComplete();
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        isRefresh = true;
        mPresenter.getMessageList(page);
    }

    @Override
    public void onLoadMore() {
        page++;
        isRefresh = false;
        mPresenter.getMessageList(page);
    }
}
